<?php

namespace App\Admin\Service;

use App\Admin\Grid\NGrid;
use App\Models\WithdrawModel;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Show;

class WithdrawService extends BaseService
{
    public string $table = 'withdraw';

    /**
     * grid
     * @return void
     */
    public function grid(): void
    {
        $this->grid = new NGrid(new WithdrawModel);
        $this->grid->model()->leftJoin('shop_user', 'shop_user.user_id', 'withdraw.user_id')
            ->orderByDesc('withdraw.id')->select(['withdraw.*', 'shop_user.user_mobile']);
        $this->filter();
        $this->gid();
        $this->column('serialNumber');
        $this->using('status');
        $this->using('paymentMethodId');
        $this->using('statusArrival');
        $this->column('userMobile,name,identity,bankName,amount,fee,okReason,failReason,remark,payAt,auditAt,createdAt');
        $this->fileId();
        $this->grid->disableActions(false);
        $this->grid->actions(function (Actions $action) {
            $action->disableDelete();
            $action->disableView(false);
            $action->disableEdit();
        });
    }

    /**
     * show
     * @param int $id
     * @return void
     */
    public function show(int $id): void
    {
        $this->id = $id;
        $this->show = new Show(WithdrawModel::query()->leftJoin('shop_user', 'shop_user.user_id', 'withdraw.user_id')
            ->leftJoin('admin_users', 'admin_users.id', 'withdraw.operate_id')
            ->orderByDesc('withdraw.id')->select(['withdraw.*', 'shop_user.user_mobile', 'admin_users.username'])->find($id));
        $this->sid();
        $this->field('serialNumber');
        $this->using('status');
        $this->using('paymentMethodId');
        $this->using('statusArrival');
        $this->field('userMobile,name,identity,bankName,amount,fee,okReason,failReason,remark,payAt,auditAt,username,createdAt');
        $this->fileId();
        $this->show->field('result', $this->ml['result'])->json();
        $this->show->field('find', $this->ml['find'])->json();
        $this->show->field('extend', $this->ml['extend'])->json();
    }

    /**
     * filter
     * @return void
     */
    public function filter(): void
    {
        $this->grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('withdraw-id', 'ID');
                $filter->equal('withdraw-status', $this->l['status'])->select($this->ml['statusArr']);
            });
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('withdraw-serial_number', __('l.serialNumber'));
            });
        });
    }
}
